package com.example.ruralatour.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.aliyunOSS.AliOSSUtils;
import com.example.common.vo.Result;
import com.example.ruralatour.entity.Routelist;
import com.example.ruralatour.service.IRoutelistService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhangzhen
 * @since 2024-05-16
 */
@RestController
@RequestMapping("/routelist")
@RequiredArgsConstructor
public class RoutelistController {
    @Autowired
    private IRoutelistService routelistService;

    @Autowired
    private AliOSSUtils aliOSSUtils;

    @GetMapping("/list")
    public Result<?> getRoutelistListPage(@RequestParam(value = "name", required = false) String name,
                                             @RequestParam("pageNo") Long pageNo,
                                             @RequestParam("pageSize") Long pageSize){

        QueryWrapper<Routelist> wrapper = new QueryWrapper<>();
        if (name != null){
            wrapper.eq("name", name);
        }

        wrapper.orderByDesc("routelistId");
        Page<Routelist> page = new Page<>(pageNo, pageSize);

        routelistService.page(page, wrapper);

        Map<String, Object> data = new HashMap<>();
        data.put("total", page.getTotal());
        data.put("rows", page.getRecords());

        return Result.success(data);
    }
    @PostMapping("/addImage")
    public  Result<?> addImage(MultipartFile file) throws IOException {
        try {
            byte[] fileBytes = file.getBytes();
            // 检查文件数组是否为空，且长度大于等于2
            if (fileBytes.length >= 2) {
                // 访问数组的第二个元素
                byte secondByte = fileBytes[1];

                // 执行文件上传到阿里云OSS
                String url = aliOSSUtils.upload(file);

                // 输出成功信息和访问URL
                System.out.println("文件上传成功，访问URL：" + url);

                // 返回成功响应，带上访问URL等信息
                return Result.success("文件上传成功", url);
            } else {
                // 处理文件数组长度小于2的情况
                return Result.fail("文件数组长度小于2");
            }
        } catch (ArrayIndexOutOfBoundsException e) {
            // 处理数组索引越界异常
            return Result.fail("访问的数组索引越界");
        } catch (Exception e) {
            // 处理其他异常
            e.printStackTrace();
            return Result.fail("文件上传失败");
        }
    }

    @PostMapping("/addRoutelist")
    public Result<?> addRoutelist(@RequestBody Routelist routelist) throws IOException {
        routelistService.save(routelist);
        return Result.success("添加旅游路线成功");
    }

    //@PutMapping
    @PostMapping("/updateRoutelist")
    public Result<?> updateRoutelist(@RequestBody Routelist routelist){
        routelistService.updateById(routelist);
        return Result.success("修改旅游路线成功");
    }

    @GetMapping("/getRoutelistById/{id}")
    public Result<Routelist> getRoutelistById(@PathVariable("id") Integer routelistId){
        Routelist routelist=routelistService.getById(routelistId);
        return Result.success(routelist);
    }

    @DeleteMapping("/deleteRoutelistById")
    public Result<Routelist> deleteRoutelistById(@RequestParam("deleteRoutelistId") Integer id){
        routelistService.removeById(id);
        return Result.success("删除旅游路线成功");
    }

}

